package com.blb.controller;


import com.blb.dto.*;
import com.blb.service.ZDanmuService;
import com.blb.service.ZLevelService;
import com.blb.service.ZUserService;
import com.blb.utils.LayUIDataTable;
import com.blb.utils.Status;
import com.blb.vo.Danmuright;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;


/**
 * zhibo
 */



@Controller
@RequestMapping("/danmu")
@CrossOrigin
public class DanmuController {

    @Autowired
    private ZDanmuService zDanmuService;

    @Autowired
    private ZUserService zUserService;

    @Autowired
    private ZLevelService zLevelService;

    @RequestMapping("/add/{uid}/{zid}/{msg}")
    @ResponseBody
    public LayUIDataTable adddanmu(Model model, @PathVariable("uid") String uid, @PathVariable("zid") int zid,@PathVariable("msg") String msg)
    {
        LayUIDataTable dt = new LayUIDataTable();
        dt.setCode(Status.FAIL);
        if(!uid.equals("null")) {           //判断是否登录
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
            String format = df.format(date).trim();
            ZDanmu zDanmu = new ZDanmu();
            zDanmu.setMessage(msg);         //添加弹幕信息
            zDanmu.setMsgdate(format);
            zDanmu.setUid(Integer.parseInt(uid));
            zDanmu.setZid(zid);
            zDanmuService.insert(zDanmu);
            dt.setCode(Status.SUCCESS);
        }
        dt.setCount(1);
        dt.setMsg("弹幕发送成功!");
        dt.setData(1);
        return dt;
    }

    @RequestMapping("/showdanmu/{zid}/{time}") //聊天室
    @ResponseBody
    public LayUIDataTable showdanmu(Model model,@PathVariable("zid") int zid,@PathVariable("time") String string)
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZDanmuExample zDanmuExample = new ZDanmuExample();
        ZDanmuExample.Criteria criteria = zDanmuExample.createCriteria();
        zDanmuExample.setOrderByClause("msgdate DESC limit 0,100");   //最多获取100条历史信息

        criteria.andZidEqualTo(zid);                                   //根据主播查询
        criteria.andMsgdateGreaterThanOrEqualTo(string);                //用户进入直播间的时间
        List<ZDanmu> zDanmus = zDanmuService.selectByExample(zDanmuExample);  //查询弹幕
        List<Danmuright> objects = new ArrayList<>();
        Collections.reverse(zDanmus);
        for (ZDanmu z:zDanmus) {                                        //转换成前端显示弹幕的格式
            ZUser zUser = zUserService.selectByPrimaryKey(z.getUid());
            Danmuright danmuright = new Danmuright();
            danmuright.setName(zUser.getNichen());
            danmuright.setLv("Lv"+zUser.getLv());
            danmuright.setMsg(z.getMessage());
            objects.add(danmuright);
        }
        dt.setCode(Status.SUCCESS);
        dt.setCount(objects.size());
        dt.setMsg("弹幕查询成功!");
        dt.setData(objects);
        return dt;
    }

    @RequestMapping("/showdanmunow/{zid}/{time}")   //屏幕弹幕
    @ResponseBody
    public LayUIDataTable showdanmunow(Model model,@PathVariable("zid") int zid,@PathVariable("time") String string)
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZDanmuExample zDanmuExample = new ZDanmuExample();
        ZDanmuExample.Criteria criteria = zDanmuExample.createCriteria();
        criteria.andZidEqualTo(zid);                            //根据主播查询
        criteria.andMsgdateEqualTo(string);                     //根据时间查询
        List<ZDanmu> zDanmus = zDanmuService.selectByExample(zDanmuExample); //查询的弹幕集合
        List objects = new ArrayList<>();
        Collections.reverse(zDanmus);
        for (ZDanmu z:zDanmus) {
            objects.add(z.getMessage());
        }
        dt.setCode(Status.SUCCESS);
        dt.setCount(objects.size());
        dt.setMsg("屏幕弹幕查询成功!");
        dt.setData(objects);
        return dt;
    }
}
